package com.yang.utils;


/**
 * @ClassName: FormatLabel
 * @Description: TODO
 * @Author: Administrator
 * @Date: 2022/3/14 18:54
 * @Version 1.0
 **/
public class FormatLabel {
    public static String formatLabel(String label) {
        int maxLength = 30;
        int labelCount = 0;
        StringBuilder strB = new StringBuilder();

        char[] c = label.toCharArray();
        for (int i = 0; i < c.length; i++) {
            String len = Integer.toBinaryString(c[i]);
            if (len.length() > 8) {
                labelCount += 2;
                strB.append(c[i]);
            } else {
                labelCount++;
                strB.append(c[i]);
            }

            if (labelCount == maxLength) {
                return strB.toString();
            } else if (labelCount > maxLength) {
                strB.setCharAt(strB.length() - 1, ' ');
                return strB.toString();
            }
        }

        //if labelCount < maxLength, add space until maxLength
        if (maxLength > labelCount) {
            int diff = maxLength - labelCount;
            for (int i = 0; i < diff; i++) {
                strB.append(' ');
            }
        }
        return strB.toString();
    }
}
